/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kweative.lingua.server.session;

import com.kweative.cluster.Cluster;
import java.util.Date;
import java.util.HashMap;

/**
 *
 * @author henrik
 */
public class Session {
    private HashMap<String,Object> data;
    private String CID;
    private String SID;
    private Date lastTouched;
    public Session(String SID) {
        this.SID = SID;
        CID = "Session_" + SID;
        data = Cluster.get(CID,new HashMap<String, Object>());
        touch();
    }
    public void set(String key,Object value) {
        data.put(key, value);
        Cluster.store(CID,data);
    }
    public Object get(String key) {
        return data.get(key);
    }
    public boolean has(String key) {
        return data.containsKey(key);
    }
    public final void touch() {
        lastTouched = new Date();
    }
    public final long getAge() {
        Date now = new Date();
        return now.getTime() - lastTouched.getTime();
    }

    public String getCID() {
        return CID;
    }

    public String getSID() {
        return SID;
    }
    public void setUser(Object user) {
        set("__USER", user);
    }
    public Object getUser() {
        return get("__USER");
    }
    public boolean hasUser() {
        return has("__USER");
    }
}
